/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
  let res = [], temp = [], visited = new Array(nums.length).fill(false);
  function dfs(depth) {
    if(depth == nums.length) {
      res.push([...temp]);
      return;
    }
    for(let i =0; i < nums.length; i++) {
      if(!visited[i]) {
        temp.push(nums[i]);
        visited[i] = true;
        dfs(depth + 1);
        visited[i] = false;
        temp.splice(temp.length - 1, 1);
      }
    }
  }
  dfs(0);
  return res;
};
let nums = [1, 2, 3];
console.log(permute(nums));
nums = [0, 1];
console.log(permute(nums));
nums = [1];
console.log(permute(nums));
